home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lock / lockcreat.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  608b  |  37 lines

  1. /*
  2.  * Locking routines using a creat() system call with all
  3.  * permissions turned off.
  4.  */
  5.  
  6. #include    <sys/errno.h>
  7. extern int    errno;
  8.  
  9. #define    LOCKFILE    "seqno.lock"
  10. #define    TEMPLOCK    "temp.lock"
  11.  
  12. my_lock(fd)
  13. int    fd;
  14. {
  15.     int    tempfd;
  16.  
  17.     /*
  18.      * Try to create a temporary file, with all write
  19.      * permissions turned off.  If the temporary file already
  20.      * exists, the creat() will fail.
  21.      */
  22.  
  23.     while ( (tempfd = creat(TEMPLOCK, 0)) < 0) {
  24.         if (errno != EACCES)
  25.             err_sys("creat error");
  26.         sleep(1);
  27.     }
  28.     close(tempfd);
  29. }
  30.  
  31. my_unlock(fd)
  32. int    fd;
  33. {
  34.     if (unlink(TEMPLOCK) < 0)
  35.         err_sys("unlink error for tempfile");
  36. }
  37.